package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.SysUserRoleService;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    SysUserRoleMapper sysUserRoleMapper;
    @Override
    public void assignRoles(Long userId, List<Long> roleIds) {
        if(userId==null){
            throw new GuiguException(ResultCodeEnum.LOGIN_PARAMS_ERROR);
        }

        //1、删除该用户所有的角色
        sysUserRoleMapper.deleteAllRolesByUserId(userId);
        //2、如果传入的roleIds不为空，为当前用户分配角色
        if(CollectionUtils.isEmpty(roleIds)){
            return;
        }
        sysUserRoleMapper.assignRoles2User(userId,roleIds);
    }
}
